﻿using System;
using System.Collections.Generic;
using System.Text;
using JWFW.SFL;

namespace JWFW.Model.Register
{
    /// <summary>
    /// 网络注册模块－客户端
    /// </summary>
    public class RegisterClient:BaseRegisterClient
    {
        /// <summary>
        /// 网络注册模块－客户端
        /// </summary>
        public RegisterClient()
        {
        }

        /// <summary>
        /// 访问WS的代理类
        /// </summary>
        protected static IValidateReg s_VR = null;
        /// <summary>
        /// 备用的WS
        /// </summary>
        protected static IValidateReg s_VR_bak = null;
        /// <summary>
        /// 设置WS的URL
        /// </summary>
        /// <param name="vr">WS代理</param>
        /// <param name="url">WS的url</param>
        public static void SetWSURL(IValidateReg vr,string url)
        {
            s_VR = vr;
            s_VR.Url = url;
        }
        /// <summary>
        /// 设置备用的WS的URL
        /// </summary>
        /// <param name="vr">备用的WS代理</param>
        /// <param name="url">备用的WS的URL</param>
        public static void SetBakWSURL(IValidateReg vr,string url)
        {
            s_VR_bak = vr;
            s_VR_bak.Url = url;
        }

        /// <summary>
        /// 随机验证时随机数的上限
        /// </summary>
        protected int g_seed = 1;



        /// <summary>
        /// 获取新版本下载地址，返回下载地址或抛异常
        /// </summary>
        /// <returns>新版本下载地址</returns>
        public string GetNewVersionUrl(string clientInfo)
        {
            lock (this)
            {
                string res = "";
                //先从第一个WS获取信息
                try
                {
                    SetTimer(false);
                    res = g_b64.Decode(s_VR.GetNewVersionUrl(clientInfo));
                }
                catch
                {
                    //网络失败后从备用WS获取信息
                    try
                    {
                        SetTimer(false);
                        res = g_b64.Decode(s_VR_bak.GetNewVersionUrl(clientInfo));
                    }
                    catch
                    {
                        //还是失败，抛出网络错误异常
                        throw new Exception("netError");
                    }
                }
                finally
                {
                    SetTimer(false);
                }
                //拿到信息后处理
                try
                {
                    SetTimer(true);
                    if (res.StartsWith(SFL.CommonOP.Spliter))
                    {
                        return g_b64.Decode(res.Substring(1));
                    }
                    else
                    {
                        throw new Exception(res);
                    }
                }
                finally
                {
                    SetTimer(false);
                }
            }
        }
        /// <summary>
        /// 创建试用帐户，返回注册码或抛异常
        /// </summary>
        /// <returns></returns>
        public string TrivialAccount(string clientInfo)
        {
            lock (this)
            {
                string res = "";
                try
                {
                    SetTimer(false);
                    res = g_b64.Decode(s_VR.TrialAccount(clientInfo));
                }
                catch
                {
                    try
                    {
                        SetTimer(false);
                        res = g_b64.Decode(s_VR_bak.TrialAccount(clientInfo));
                    }
                    catch
                    {
                        throw new Exception("netError");
                    }
                }
                finally
                {
                    SetTimer(false);
                }
                try
                {
                    SetTimer(true);
                    if (res.StartsWith(SFL.CommonOP.Spliter))
                    {
                        return g_b64.Decode(res.Substring(1));
                    }
                    else
                    {
                        throw new Exception(res);
                    }
                }
                finally
                {
                    SetTimer(false);
                }
            }
        }
        /// <summary>
        /// 验证帐户信息，失败抛异常
        /// </summary>
        public void ValidateAccount(string clientInfo)
        {
            lock (this)
            {
                string res = "";
                try
                {
                    SetTimer(false);
                    res = g_b64.Decode(s_VR.ValidateAccount(clientInfo));
                }
                catch
                {
                    try
                    {
                        SetTimer(false);
                        res = g_b64.Decode(s_VR_bak.ValidateAccount(clientInfo));
                    }
                    catch
                    {
                        throw new Exception("netError");
                    }
                }
                finally
                {
                    SetTimer(false);
                }
                try
                {
                    SetTimer(true);                    
                    if (res.StartsWith(SFL.CommonOP.Spliter))
                    {
                        this.ParseInfo(g_b64.Decode(res.Substring(1)));
                        this.HandleRespons();
                    }
                    else
                    {
                        throw new Exception(res);
                    }
                }
                finally
                {
                    SetTimer(false);
                }
            }
        }
        /// <summary>
        /// 检查帐户信息，并返回工具运行时需要的信息
        /// </summary>
        /// <returns></returns>
        public string GetInfo(string clientInfo,int index)
        {
            string vlu = "";
            SetTimer(true);
            //随机清空
            RandomReg();
            vlu = this.GetDictionary(index.ToString());
            if (vlu == "")//取不到值，需要从网络获取
            {
                lock (this)
                {
                    string res = "";
                    try
                    {
                        SetTimer(false);
                       res = g_b64.Decode(s_VR.GetInfo(clientInfo));
                    }
                    catch
                    {
                        try
                        {
                            SetTimer(false);
                            res = g_b64.Decode(s_VR_bak.GetInfo(clientInfo));
                        }
                        catch
                        {
                            throw new Exception("netError");
                        }
                    }
                    finally
                    {
                        SetTimer(false);
                    }
                    try
                    {
                        SetTimer(true);
                        if (res.StartsWith(SFL.CommonOP.Spliter))
                        {
                            this.ParseInfo(g_b64.Decode(res.Substring(1)));
                            vlu = this.GetDictionary(index.ToString());
                        }
                        else
                        {
                            throw new Exception(res);
                        }
                    }
                    finally
                    {
                        SetTimer(false);
                    }
                }
            }
            return vlu;
        }
        /// <summary>
        /// 解析服务端发回的信息
        /// </summary>
        /// <param name="respons">服务端发回的信息</param>
        protected void ParseInfo(string respons)
        {
            this.SetTimer(true);
            string[] res = respons.Split(CommonOP.Spliter.ToCharArray());
            this.AddDictioanry("user", res[0].Trim());
            this.AddDictioanry("rc", res[1].Trim());
            string[] infos = res[2].Split(';');
            g_seed = int.Parse(infos[0].Trim());
            for (int i = 1; i < infos.Length; i++)
            {
                this.AddDictioanry(i.ToString(), infos[i].Trim());
            }
        }
        /// <summary>
        /// 处理解析后的服务端发回信息
        /// </summary>
        protected void HandleRespons()
        {
            this.SetTimer(true);
            if (this.GetDictionary("originUser") != this.GetDictionary("user"))
            {
                throw new Exception("fail");
            }
            if (this.GetDictionary("originRc") != this.GetDictionary("rc"))
            {
                throw new Exception("fail");
            }
        }
        /// <summary>
        /// 随机触发注册
        /// </summary>
        protected void RandomReg()
        {
            this.SetTimer(true);
            Random r = new Random();
            if (r.Next(g_seed) < Math.Max(5, g_hash.Count))
            {
                g_hash.Clear();
            }
        }
    }
}
